package com.koron.system.common.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.koron.common.core.business.configsettings.utils.ConfigInfoTool;
import com.koron.common.core.util.HttpClient;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

/**
 * @ClassName UnifyPlatUtils
 * @description
 * @Author zhouj
 * @Date 2021/7/8 14:11
 */
@Slf4j
public class UnifyPlatUtils {

    private static HttpClient httpClient = new HttpClient();

    private UnifyPlatApi unifyPlatApi = new UnifyPlatApi();

    public JSONArray getOrgNode(Map<String, String> cond) {

        String res = httpClient.doGet(unifyPlatApi.getPORT_ORGCODE(), cond);

        return parseOrg(res);
    }

    public JSONArray getStaff(Map<String, String> cond) {

        String res = httpClient.doGet(unifyPlatApi.getPORT_STAFF(), cond);

        return parseStaff(res);
    }

    private JSONArray parseOrg(String res) {
        JSONObject jsonObject = JSONObject.parseObject(res);
        Integer code = jsonObject.getInteger("code");
        if (code == 0) {
            return jsonObject.getJSONArray("data");
        } else {
            log.error("统一用户平台调用失败，返回码：{},描述：{}", code, jsonObject.getString("description"));
        }
        return new JSONArray();
    }

    private JSONArray parseStaff(String res) {
        JSONObject jsonObject = JSONObject.parseObject(res);
        Integer code = jsonObject.getInteger("code");
        if (code == 0) {
            Map<String, String> map = ConfigInfoTool.getConfigByCode("system", "org");
            String environment = map.get("environment");

            switch (environment) {
                case "eam":
                    return jsonObject.getJSONArray("data");
                case "yc":
                    return jsonObject.getJSONObject("data").getJSONArray("list");
                default:
                    return new JSONArray();
            }
        } else {
            log.error("统一用户平台调用失败，返回码：{},描述：{}", code, jsonObject.getString("description"));
        }
        return new JSONArray();
    }


}
